﻿using MyAspx.EasyFramework.DAL;
using MyAspx.EasyFramework.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebUI.Form
{
    public partial class CodesMarket : System.Web.UI.Page
    {
        DBAccess db = new DBAccess();
        public commyaspxdbEntities _commyaspxdbEntities = new commyaspxdbEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            Title = "源码市场,Asp.net源码|- MyAspx.com";
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }
        public string ImgUrl(string imgs)
        {
           string[] imgArray = imgs.Split('>');
            string firImg = imgs[0].ToString();
            firImg = firImg.Replace("<br", "");
            return Server.MapPath(firImg);
        }

        protected void RepeaterCodeLists_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var fid = DataBinder.Eval(e.Item.DataItem, "fileid");
                var usemsg = DataBinder.Eval(e.Item.DataItem, "usermsg");
                int saletypeid = int.Parse(DataBinder.Eval(e.Item.DataItem, "saletypeid").ToString());
                //绑定浏览量
                int codeCount = 0;
                codeCount = _commyaspxdbEntities.my_browse.Where(x => x.fileid == fid).Count() > 0 ? int.Parse(_commyaspxdbEntities.my_browse.Where(x => x.fileid == fid).First().browsecount.ToString()) : 0;
                dynamic label = e.Item.FindControl("labprw") as dynamic;
                if (label != null)
                    label.InnerText = codeCount.ToString();

                int downCount = 0;
                downCount = _commyaspxdbEntities.my_downdetail.Where(x => x.fileid == fid).Count() > 0 ? int.Parse(_commyaspxdbEntities.my_downdetail.Where(x => x.fileid == fid).First().downcount.ToString()) : 0;

                dynamic labeldown = e.Item.FindControl("labdown") as dynamic;
                if (labeldown != null)
                    labeldown.InnerText = downCount.ToString();


                string salestr = "";
                //源码销售类型和金额
                try
                {
                    var stquery = /*from*/ saletypeid;

                    switch (stquery)
                    {
                        case 1:
                            salestr = "<font style='color:green;font-size:22px'>免费</font>";
                            break;
                        case 2:
                            salestr = "积分";
                            var salenub =
                                _commyaspxdbEntities.my_saledetail.Where(sd => sd.fileid == fid)
                                    .First()
                                    .salenum;
                            salestr = "<font style='color:#F6BB42;font-size:22px'>" + salenub + " 积分" + "</font>";
                            break;
                        case 4:
                            salestr = "人民币";
                            var salenub2 =
                             _commyaspxdbEntities.my_saledetail.Where(sd => sd.fileid == fid)
                                 .First()
                                 .salenum;
                            salestr = "<font style='color:#DA4453;font-size:22px'>" + salenub2 + " 人民币" + "</font>";

                            break;
                        case 3:
                            salestr = "人民币";
                            var salenub3 =
                              _commyaspxdbEntities.my_saledetail.Where(sd => sd.fileid == fid)
                                  .First()
                                  .salenum;
                            salestr = "<font style='color:#DA4453;font-size:22px'>" + salenub3 + " 人民币" + "</font>";
                            break;
                    }


                }
                catch (Exception) { salestr = "<font style='color:silver;font-size:15px'>免费/限时</font>"; }
                dynamic labelsaletype = e.Item.FindControl("labsaletype") as dynamic;
                if (labelsaletype != null)
                    labelsaletype.InnerHtml = salestr;

                //<p class="borderrightblank" style="margin-left: -100px;font-variant:normal;line-height: 18px;text-align: left;background-color: #fa8072;margin-bottom: 10px;padding-left: 4px;"></p>
                dynamic labelusermsg = e.Item.FindControl("labusermsg") as dynamic;
                if (labelusermsg != null)
                {
                    if (usemsg.ToString() == ""/*null || Convert.ToString(usemsg)  == ""*/)
                    {
                        labelusermsg.InnerHtml = "<p class=\"borderrightblank\" style=\"margin-left: -100px;font-variant:normal;line-height: 18px;text-align: left;background-color: green;margin-bottom: 10px;padding-left: 4px;\">这个上传者很懒,没有写源码摘要!</p>";
                    }
                    else
                    {
                        labelusermsg.InnerHtml = "<p class=\"borderrightblank\" style=\"margin-left: -100px;font-variant:normal;line-height: 18px;text-align: left;background-color: #fa8072;margin-bottom: 10px;padding-left: 4px;\">" + usemsg + "</p>";
                    }
                }

                dynamic labeluserdata = e.Item.FindControl("labuserdata") as dynamic;
                if (labeluserdata != null)
                {
                    if (_commyaspxdbEntities.my_usercodererelation.Where(fi => fi.fileid == fid).Count() > 0)
                    {
                        if (
                            _commyaspxdbEntities.my_users.Where(
                                u =>
                                    u.userid ==
                                    _commyaspxdbEntities.my_usercodererelation.Where(fi => fi.fileid == fid)
                                        .FirstOrDefault()
                                        .userid).Count() > 0)
                            labeluserdata.InnerHtml = "<img src='" +
                                                      _commyaspxdbEntities.my_users.Where(
                                                          u =>
                                                              u.userid ==
                                                              _commyaspxdbEntities.my_usercodererelation.Where(
                                                                  fi => fi.fileid == fid).FirstOrDefault().userid)
                                                          .FirstOrDefault()
                                                          .uselogopic + "' style='width:18px;height:18px' title='" + _commyaspxdbEntities.my_users.Where(
                                                          u =>
                                                              u.userid ==
                                                              _commyaspxdbEntities.my_usercodererelation.Where(
                                                                  fi => fi.fileid == fid).FirstOrDefault().userid)
                                                          .FirstOrDefault().useregname + "' />";
                        else
                            labeluserdata.InnerHtml = "<img title='系统管理员上传的源码！'  src='" + "/images/admin/login-user-logo.png" + "' style='width:18px;height:18px' />";

                    }

                    else
                    {
                        labeluserdata.InnerHtml = "<img title='系统管理员上传的源码！' src='" + "/images/admin/login-user-logo.png" + "' style='width:18px;height:18px' />";
                    }
                }
                //else
                //{
                //    labeluserdata.InnerHtml = "<img src='" + "/images/avater/d1.png" + "' style='width:18px;height:18px' />";
                //}
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        public void BindGrid()
        {
            if (Request.QueryString.Keys.Count > 0 && Request.QueryString.Keys[0] != "page")
            {
                string wheresql0 = " ";
                for (int i = 0; i < Request.QueryString.Count; i++)
                {
                    if (Request.QueryString.Keys[i] != "page")
                    {
                        if (Request.QueryString.Get(i) != "csharp")
                            wheresql0 += Request.QueryString.Keys[i] + "='" + Request.QueryString.Get(i) + "' and ";
                        else
                            wheresql0 += Request.QueryString.Keys[i] + "='" + "C#" + "' and ";
                    }
                    // wheresql0 += Request.QueryString.Keys[i] + "='" + Request.QueryString.Get(i) + "' and ";
                }
                wheresql0 = wheresql0.Substring(0, wheresql0.Length - 4);
                AspNetPager1.RecordCount = Int32.Parse(db.GetAllCount("my_fileinfo", wheresql0).ToString());
            }
            else
            {
                AspNetPager1.RecordCount = Int32.Parse(db.GetAllCount("my_fileinfo").ToString());
            }

            //a
            int pageIndex = AspNetPager1.CurrentPageIndex - 1;
            int pageSize = AspNetPager1.PageSize = 15;
            if (Request.QueryString.Keys.Count <= 1)
            {
                if (Request.QueryString.Keys.Count > 0 && Request.QueryString.Keys[0] != "page")
                {
                    RepeaterCodeLists.DataSource = db.GetCurrentPage2(pageIndex, pageSize, "my_fileinfo", "addtime", Request.QueryString.Keys[0] + "= " + Request.QueryString.Get(0) +" ");
                }
                else
                {
                    RepeaterCodeLists.DataSource = db.GetCurrentPage(pageIndex, pageSize, "my_fileinfo", "addtime");
                }
                
            }
            else
                if (Request.QueryString.Keys.Count <= 0)
                {
                    RepeaterCodeLists.DataSource = db.GetCurrentPage(pageIndex, pageSize, "my_fileinfo", "addtime");
                }
                else
                {
                    string wheresql = "";
                    for (int i = 0; i < Request.QueryString.Count; i++)
                    {
                        if (Request.QueryString.Keys[i] != "page")
                        {
                            if (Request.QueryString.Get(i) != "csharp")
                                wheresql += Request.QueryString.Keys[i] + "='" + Request.QueryString.Get(i) + "' and ";
                            else
                                wheresql += Request.QueryString.Keys[i] + "='" + "C#" + "' and ";
                        }

                    }
                    wheresql = wheresql.Substring(0, wheresql.Length - 4);
                    RepeaterCodeLists.DataSource = db.GetCurrentPage2(pageIndex, pageSize, "my_fileinfo", "addtime", wheresql);
                }
            RepeaterCodeLists.DataBind();
        }
    }
}